<?php
class Application {
	
	//Model请求
	private $module;
	
	//Action请求
	private $action;
	
	//URI转发器
	public $route;
	
	const PATH_SEPERATOR = '/';
	
	/**
	 * 构造函数.
	 * 
	 * @return void
	 * @access public
	 * 
	 */
	public function __construct() {
		if (DEBUG === TRUE) {
			echo '--- Application construct!' . '<br/>';
		}
	}
	
	/**
	 * Run the application
	 *    
	 * @return void
	 * @access public
	 *
	 */
	public function run($start = TRUE) {
		$this->route ();
		if ($start === TRUE) {
			$this->setHeader ();
			$this->Control ( $_GET ['controller'] . '/' . $_GET ['action'] );
			if (DEBUG === TRUE) { echo "<h3>加载模块：{$_GET ['module']} 执行动作：{$_GET ['action']}</h3>"; }
		}
		$this->filterRequest ();
	}
	
	public function route() {
		Mid::loadClass ( 'route' );
		new Route ( $this->route );
		$route = $this->route;
		if (isset ( $route ['/'] )) {
			$defaultModule = $defaultAction = NULL;
			@list ( $defaultModule, $defaultAction ) = explode ( '/', $route ['/'] );
		}
		$defaultModule = isset ( $defaultModule ) ? $defaultModule : DEFAULT_MODULE;
		$defaultAction = isset ( $defaultAction ) ? $defaultAction : DEFAULT_ACTION;
		$_GET ['controller'] = $this->module = isset ( $_GET ['controller'] ) && $_GET ['controller'] != '' ? $_GET ['controller'] : $defaultModule;
		$_GET ['action'] = $this->action = isset ( $_GET ['action'] ) && $_GET ['action'] != '' ? $_GET ['action'] : $defaultAction;
		return $this;
	}
	
	/**
	 * control module
	 *   example: Controller('index/index');
	 * @param string $path
	 */
	public function Control($path, $param = NULL) {
		static $loaded = NULL;
		if ($loaded === NULL) {
			Mid::loadClass ( 'controller' );
			$loaded = true;
		}
		list ( $controllerName, $actionName ) = explode ( self::PATH_SEPERATOR, $path );
		Mid::load ( "m/controller/" . $controllerName );
		$controllerName = strtolower ( $controllerName ) . "Controller";
		$actionName = 'action' . ucfirst ( strtolower ( $actionName ) );
		if (! class_exists ( $controllerName )) {
			exit ( "Application Error: Count not found controller Class(" . $controllerName . ")" );
		} else {
			$controller = new $controllerName ( );
			if (method_exists ( $controller, $actionName ) && is_callable ( array ($controllerName, $actionName ) )) {
				$controller->$actionName ();
			} else {
				exit ( "Application Error: Count not found Action method or not callable [Class Name:{$controllerName}Controller Method Name:{$actionName}]!" );
			}
		}
		//return $controller;
	}
	
	/**
	 * Filter request string.
	 *
	 */
	protected function filterRequest() {
		! defined ( 'MAGIC_QUOTES_GPC' ) && define ( 'MAGIC_QUOTES_GPC', get_magic_quotes_gpc () );
		if (MAGIC_QUOTES_GPC)
			return;
		$this->addSlash ( $_POST );
		$this->addSlash ( $_GET );
		$this->addSlash ( $_COOKIE );
		$this->addSlash ( $_FILES );
	}
	
	/**
	 * add slashes to string or array
	 *
	 * @param string $string
	 * @param unknown_type $force
	 * @return string addslashed
	 */
	public function addSlash(&$string) {
		if (is_array ( $string )) {
			foreach ( $string as $key => $value ) {
				is_array ( $value ) ? $this->addSlash ( $value ) : $string [$key] = addslashes ( $value );
			}
		} else {
			$string = addslashes ( $value );
		}
	}
	
	/**
	 * set public header
	 * 
	 * @return void
	 * @access private
	 *
	 */
	protected function setHeader() {
		header ( "HTTP/1.0 503 Service Unavailable" );
		header ( 'X-Powered-By: ' . substr ( $_SERVER ['SERVER_ADDR'], - 3 ) );
		header ( 'X-PVer: MidPHP Framework' );
	}
	
	/**
	 * throw error message
	 *
	 * @param string $message
	 * @return void
	 */
	protected function throwException($message) {
		throw new Exception ( $message );
	}
}
?>